//
//  ComputingPowerController.swift
//  SCA
//
//  Created by tjun on 2025/9/1.
//

import UIKit
import SnapKit

class ComputingPowerController: BaseController {
    
    private var scrollView: UIScrollView!
    private let overviewButton = SelectableGradientLineButton()
    private let powerListButton = SelectableGradientLineButton()

    override func viewDidLoad() {
        super.viewDidLoad()

        setNavigationBarUI()
        
        
        let segmentView = UIView().addTo(view)
        segmentView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalTo(navBarHeight + 10)
            make.height.equalTo(40)
        }
        
        overviewButton.addTo(segmentView)
        overviewButton.setTitle("总览".localizedString, for: .normal)
        overviewButton.selectedColor = .white
        overviewButton.normalColor = .lightGray
        overviewButton.underlineColor = .white
        overviewButton.titleLabel?.font = UIFontMake(14)
        overviewButton.addTarget(self, action: #selector(overviewButtonTapped), for: .touchUpInside)
        overviewButton.isSelected = true
        overviewButton.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.width.equalTo((screenWidth - AppStyle.margin.left * 2) * 0.5)
            make.height.top.equalToSuperview()
        }
        
        powerListButton.addTo(segmentView)
        powerListButton.setTitle("算力列表".localizedString, for: .normal)
        powerListButton.selectedColor = .white
        powerListButton.normalColor = .lightGray
        powerListButton.underlineColor = .white
        powerListButton.isSelected = false
        powerListButton.titleLabel?.font = UIFontMake(14)
        powerListButton.addTarget(self, action: #selector(powerListButtonTapped), for: .touchUpInside)
        powerListButton.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.width.equalTo(overviewButton)
            make.height.top.equalToSuperview()
        }
  
        scrollView = UIScrollView(frame: CGRect(x: 0, y: 50 + navBarHeight, width: view.width, height: screenHeight - navBarHeight - tabBarHeight - 50)).addTo(view)
        scrollView.showsVerticalScrollIndicator = false
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.contentInsetAdjustmentBehavior = .never
        scrollView.isPagingEnabled = true
        scrollView.delegate = self
        scrollView.addTo(view)
        scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 2, scrollView.frame.size.height)
        
        let overviewController = OverviewController(realSize: CGSizeMake(scrollView.width, scrollView.height))
        addChild(overviewController)
        scrollView.addSubview(overviewController.view)
        overviewController.view.frame = CGRectMake(0, 0, scrollView.width, scrollView.height)
        
        let powerListController = PowerListController()
        addChild(powerListController)
        scrollView.addSubview(powerListController.view)
        powerListController.view.frame = CGRectMake(scrollView.width, 0, scrollView.width, scrollView.height)

    }
    
    private func setNavigationBarUI() {
        
        navigationBar.title = nil
        
        let infoButton = UIButton(image: UIImage(named: "nav_mine_info"), target: self, action: #selector(infoButtonTapped))
        navigationBar.contentView.addSubview(infoButton)
        infoButton.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(40)
        }
        
        let logoImageView = UIImageView(image: UIImage(named: "nav_logo"))
        navigationBar.contentView.addSubview(logoImageView)
        logoImageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        let logoutButton = UIButton(image: UIImage(named: "nav_logout"), target: self, action: #selector(logoutButtonTapped))
        navigationBar.contentView.addSubview(logoutButton)
        logoutButton.snp.makeConstraints { make in
            make.right.equalTo(-10)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(40)
        }
    }
    
    @objc func infoButtonTapped() {
        let mineController = MineController()
        mineController.showInController(self)
    }
    
    @objc func logoutButtonTapped() {
        NotificationCenter.default.post(name: NotificationName.userLogout, object: nil)        
    }
    
    @objc func overviewButtonTapped() {
        overviewButton.isSelected = true
        powerListButton.isSelected = false
        self.scrollView.contentOffset = CGPointMake(0, 0)
    }
    
    @objc func powerListButtonTapped() {
        powerListButton.isSelected = true
        overviewButton.isSelected = false
        self.scrollView.contentOffset = CGPointMake(self.scrollView.width, 0)
    }
}


extension ComputingPowerController: UIScrollViewDelegate {
   func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
       
       let selectedIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width)
       printf("selectedIndex=\(selectedIndex)")
       overviewButton.isSelected = selectedIndex == 0
       powerListButton.isSelected = !overviewButton.isSelected
   }
   
}

